package com.yc.upload;

import com.yc.upload.vo.UploadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@AutoConfiguration
@ComponentScan("com.yc.upload")
@ConditionalOnProperty(prefix = "upload", name="enable")
public class UploadConfig implements WebMvcConfigurer {

    @Autowired
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getMapping().forEach((k,v)->{
            // k => iamge  v => f:/...
            registry.addResourceHandler("/" + k + "/**")
                    .addResourceLocations("file:" + v + "/");
        });
    }
}
